import { createAppSlice } from "../utils";

export interface CounterSliceState {
  value: number;
  status: "idle" | "loading" | "failed";
}

const initialState: CounterSliceState = {
  value: 0,
  status: "idle",
};

export const appSlice = createAppSlice({
  name: "appStore",
  initialState,
  reducers: (create) => ({
    increment: create.reducer((state) => {
      state.value += 1;
    }),
    decrement: create.reducer((state) => {
      if (state.value === 0) return;
      state.value -= 1;
    }),
  }),
  selectors: {
    selectCount: (counter) => counter.value,
    selectStatus: (counter) => counter.status,
  },
});

export const { decrement, increment } = appSlice.actions;
export const { selectCount, selectStatus } = appSlice.selectors;
